#!/bin/bash
# docker镜像/容器名字
SERVER_NAME=yzh-order-export
# 操作/项目路径(Dockerfile和Jar存放的路径)
BASE_PATH=/opt/$SERVER_NAME/resources

# 端口号
APP_PORT=8080 #应用的端口
E_PORT=8080 #服务暴露给外部调用的端口
# 容器id
CID=$(docker ps -a | grep "$SERVER_NAME" | awk \'{print $1}\')
# 镜像id
IID=$(docker images | grep "$SERVER_NAME" | awk \'{print $3}\')

function dockerBuild(){
  if [ -n "$CID" ]; then
    echo "存在$SERVER_NAME容器，CID=$CID,删除容器 ..."
      docker rm -f $CID
    echo "$SERVER_NAME容器删除完成..."
  else
    echo "不存在$SERVER_NAME容器..."
  fi


  if [ -n "$IID" ]; then
    echo "存在$SERVER_NAME镜像，IID=$IID"
        docker rmi -f $IID
        echo "镜像删除完成"
    cd $BASE_PATH
    docker build -t $SERVER_NAME .
        echo "镜像构建完成"
  else
    echo "不存在$SERVER_NAME镜像，开始构建镜像"
    cd $BASE_PATH
        docker build -t $SERVER_NAME .
        echo "镜像构建完成"
  fi

    echo "启动容器..."
    docker run -it -v /opt/$SERVER_NAME/db:/opt/$SERVER_NAME/db -v /opt/$SERVER_NAME/logs:/opt/$SERVER_NAME/logs --name $SERVER_NAME -d -p $E_PORT:$APP_PORT $SERVER_NAME
    echo "容器启动完成"
}

dockerBuild	# 4：停止->删除旧容器，删除旧镜像，重建镜像，run容器